package com.cn.clp.controller;

import com.cn.clp.logger.BaseLogger;
import com.cn.clp.service.Server.ActiveMQServer;
import com.cn.clp.service.Server.ConsumerService;
import com.cn.clp.service.Server.ProducerService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/activeMq")
public class ActiveMQController extends BaseLogger{
    @Autowired
    private Destination demoQueueDestination;
    //队列消息生产者
    @Autowired
    private ProducerService producer;

    //队列消息消费者
    @Autowired
    private ConsumerService consumer;

    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    public ModelAndView welcome() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("welcome");
        return mv;
    }

    @RequestMapping(value = "/producer", method = RequestMethod.GET)
    public ModelAndView producer() {

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format(now);
        System.out.println(time);

        ModelAndView mv = new ModelAndView();
        mv.addObject("time", time);
        mv.setViewName("pages/jmsProducer");
        return mv;
    }

    @RequestMapping(value = "/onSend", method = RequestMethod.POST)
    public ModelAndView producer(@RequestParam("message") String message) {
        ModelAndView mv = new ModelAndView();
        producer.sendMessage(demoQueueDestination, message);
        mv.setViewName("welcome");
        return mv;
    }

    @RequestMapping(value = "/receive", method = RequestMethod.GET)
    public ModelAndView queue_receive() throws JMSException {
        ModelAndView mv = new ModelAndView();

        TextMessage tm = consumer.receive(demoQueueDestination);
        if(tm == null){
            mv.addObject("textMessage", "没有收到消息");
        }else{
            mv.addObject("textMessage", tm.getText());
        }
        mv.setViewName("pages/queueReceive");
        return mv;
    }

    /*
     * ActiveMQ Manager Test
     * url : service:jmx:rmi:///jndi/rmi://127.0.0.1:8888/server
     */
    @RequestMapping(value = "/jms", method = RequestMethod.GET)
    public ModelAndView jmsManager(HttpServletRequest request) throws IOException {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("welcome");
        String url = request.getParameter("url");
       try {
           JMXServiceURL urls = new JMXServiceURL(url);
           JMXConnector connector = JMXConnectorFactory.connect(urls);
           connector.connect();
           MBeanServerConnection connection = connector.getMBeanServerConnection();
       }catch (Exception e){
          logger.error(e.getMessage().toLowerCase());
       }

        return mv;
    }

}
